using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SurveyHost.Core.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Answer class.
	/// </summary>
	[Serializable]
	public partial class AnswerCollection : ActiveList<Answer, AnswerCollection> 
	{	   
		public AnswerCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Answer table.
	/// </summary>
	[Serializable]
	public partial class Answer : ActiveRecord<Answer>
	{
		#region .ctors and Default Settings
		
		public Answer()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Answer(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Answer(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Answer(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Answer", TableType.Table, DataService.GetInstance("SurveyHost"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarSurveyId = new TableSchema.TableColumn(schema);
				colvarSurveyId.ColumnName = "SurveyId";
				colvarSurveyId.DataType = DbType.Guid;
				colvarSurveyId.MaxLength = 0;
				colvarSurveyId.AutoIncrement = false;
				colvarSurveyId.IsNullable = false;
				colvarSurveyId.IsPrimaryKey = true;
				colvarSurveyId.IsForeignKey = true;
				colvarSurveyId.IsReadOnly = false;
				colvarSurveyId.DefaultSetting = @"";
				
					colvarSurveyId.ForeignKeyTableName = "Survey";
				schema.Columns.Add(colvarSurveyId);
				
				TableSchema.TableColumn colvarStemId = new TableSchema.TableColumn(schema);
				colvarStemId.ColumnName = "StemId";
				colvarStemId.DataType = DbType.Guid;
				colvarStemId.MaxLength = 0;
				colvarStemId.AutoIncrement = false;
				colvarStemId.IsNullable = false;
				colvarStemId.IsPrimaryKey = true;
				colvarStemId.IsForeignKey = true;
				colvarStemId.IsReadOnly = false;
				colvarStemId.DefaultSetting = @"";
				
					colvarStemId.ForeignKeyTableName = "Stem";
				schema.Columns.Add(colvarStemId);
				
				TableSchema.TableColumn colvarAnswerText = new TableSchema.TableColumn(schema);
				colvarAnswerText.ColumnName = "AnswerText";
				colvarAnswerText.DataType = DbType.String;
				colvarAnswerText.MaxLength = 500;
				colvarAnswerText.AutoIncrement = false;
				colvarAnswerText.IsNullable = false;
				colvarAnswerText.IsPrimaryKey = false;
				colvarAnswerText.IsForeignKey = false;
				colvarAnswerText.IsReadOnly = false;
				colvarAnswerText.DefaultSetting = @"";
				colvarAnswerText.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAnswerText);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SurveyHost"].AddSchema("Answer",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("SurveyId")]
		public Guid SurveyId 
		{
			get { return GetColumnValue<Guid>("SurveyId"); }

			set { SetColumnValue("SurveyId", value); }

		}

		  
		[XmlAttribute("StemId")]
		public Guid StemId 
		{
			get { return GetColumnValue<Guid>("StemId"); }

			set { SetColumnValue("StemId", value); }

		}

		  
		[XmlAttribute("AnswerText")]
		public string AnswerText 
		{
			get { return GetColumnValue<string>("AnswerText"); }

			set { SetColumnValue("AnswerText", value); }

		}

		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Stem ActiveRecord object related to this Answer
		/// 
		/// </summary>
		public SurveyHost.Core.DAL.Stem Stem
		{
			get { return SurveyHost.Core.DAL.Stem.FetchByID(this.StemId); }

			set { SetColumnValue("StemId", value.StemId); }

		}

		
		
		/// <summary>
		/// Returns a Survey ActiveRecord object related to this Answer
		/// 
		/// </summary>
		public SurveyHost.Core.DAL.Survey Survey
		{
			get { return SurveyHost.Core.DAL.Survey.FetchByID(this.SurveyId); }

			set { SetColumnValue("SurveyId", value.SurveyId); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varSurveyId,Guid varStemId,string varAnswerText)
		{
			Answer item = new Answer();
			
			item.SurveyId = varSurveyId;
			
			item.StemId = varStemId;
			
			item.AnswerText = varAnswerText;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varSurveyId,Guid varStemId,string varAnswerText)
		{
			Answer item = new Answer();
			
				item.SurveyId = varSurveyId;
				
				item.StemId = varStemId;
				
				item.AnswerText = varAnswerText;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string SurveyId = @"SurveyId";
			 public static string StemId = @"StemId";
			 public static string AnswerText = @"AnswerText";
						
		}

		#endregion
	}

}

